package com.example.runnable;

import com.example.mapper.BasicMapper;
import com.example.pojo.dto.ImportDTO;
import com.github.crab2died.ExcelUtils;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

import static com.example.runnable.Constants.W_PATH;

@Component
@ConditionalOnProperty(name = "run-env",havingValue = "test02")
public class Test02 implements ApplicationRunner {

    @Resource
    SqlSessionFactory sqlSessionFactory;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        BasicMapper mapper = sqlSession.getMapper(BasicMapper.class);
        long begin=System.currentTimeMillis();
        List<ImportDTO> importDTOS = ExcelUtils.getInstance().readCSV2Objects(W_PATH, ImportDTO.class);
//        try {
//            mapper.insertDataByDTOList(importDTOS,"T_IMPORT","ID,NAME,AGE");
//            sqlSession.commit();
//            sqlSession.clearCache();
//            System.out.println("耗时:"+(System.currentTimeMillis()-begin));
//        } catch (Exception e) {
//            e.printStackTrace();
//        }finally {
//            sqlSession.close();
//        }
        System.out.println();
    }
}
